# -*- coding: utf-8 -*-
# @Time : 2018/12/24 10:14
# @Author : Administrator
# @File : httptestV1.py
# @Project : 4.Python
# @title : httplib库 -get

# 建立会话四大核心过程：建立连接、发送请求、响应请求、关闭连接
import http.client
import pymysql
class Http_Test():
    def get_test(self):  #agileone首页--GET
        # 建立连接
        con = http.client.HTTPConnection(host='192.168.2.124',port=80)
        # 发送请求
        con.request(method='GET',url='/agileone/')
        # 获取响应
        print(con.getresponse().read().decode())#decode(),默认utf-8
        # 关闭连接
        con.close()

    def post_test001(self):  #agileone登录，正确的用户名、正确的密码
        # 建立连接
        con = http.client.HTTPConnection(host='localhost', port=80)
        # 发送请求
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=admin&password=admin&savelogin=true',
                    headers={'Content-Type':'application/x-www-form-urlencoded'})
        # 获取响应
        # print(con.getresponse().read().decode())
        actural = con.getresponse().read().decode()
        # 断言正确的用户名、正确的密码，是否登录成功
        assert 'successful'==actural
        # 关闭连接
        con.close()
    def post_test002(self):  #agileone登录，空用户名、空密码
        #建立连接
        con = http.client.HTTPConnection(host='127.0.0.1',port=80)
        #发送请求
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=&password=&savelogin=true',
                    headers={"Content-Type":"application/x-www-form-urlencoded"})
        # print(con.getresponse().read().decode())
        #断言空用户名、空密码，是否登录失败
        actual = con.getresponse().read().decode()
        assert actual == 'user_invalid'
        #关闭连接
        con.close()
    def post_test003(self):  #agileone登录，正确的用户名、空密码
        #建立连接
        con = http.client.HTTPConnection(host='127.0.0.1',port=80)
        #发送请求
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=admin&password=&savelogin=true',
                    headers={"Content-Type":"application/x-www-form-urlencoded"})
        # print(con.getresponse().read().decode())
        # 断言正确的用户名、空密码，是否登录失败
        actural=con.getresponse().read().decode()
        assert "password_invalid"==actural
        #关闭连接
        con.close()
    def post_test004(self): #agileone登录，正确的用户名、错误的密码
        #建立连接
        con= http.client.HTTPConnection(host='localhost',port=80)
        #发送请求
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body="username=admin&password=aa&savelogin=true",
                    headers={"Content-Type":"application/x-www-form-urlencoded"})
        # print(con.getresponse().read().decode())
        #断言正确的用户名、错误的密码，是否登录失败
        actural = con.getresponse().read().decode()
        assert "password_invalid" == actural
        #关闭连接
        con.close()
    def post_test005(self):  #agileone登录，空用户名、正确的密码
        #新建连接
        con=http.client.HTTPConnection(host='localhost',port=80)
        #发送请求
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=&password=admin&savelogin=true',
                    headers={"Content-Type":"application/x-www-form-urlencoded"})
        # print(con.getresponse().read().decode())
        #断言空用户名、正确的密码,是否新增失败
        actual = con.getresponse().read().decode()
        assert actual == 'user_invalid'
        #关闭连接
        con.close()
    def post_test006(self):  #agileone登录，错误的用户名
        #建立连接
        con=http.client.HTTPConnection(host='localhost')
        #发送请求
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=ad&password=admin&savelogin=true',
                    headers={"Content-Type":"application/x-www-form-urlencoded"})
        # print(con.getresponse().read().decode())
        #断言错误的用户名，是否登录失败
        actual = con.getresponse().read().decode()
        assert actual == 'user_invalid'
        #关闭连接
        con.close()
    def post_test007(self):   #agileone新增公告
        #创建数据库连接对象
        db = pymysql.connect("localhost", "root", "", "agileone", charset='utf8')
        #建立游标
        cur = db.cursor()
        #获去新增前最大编号的数据
        before_sql = "select * from notice order by noticeid desc limit 0,1"
        cur.execute(before_sql)
        before_result = cur.fetchall()
        # print(before_result)
        # print(before[0][0])
        # 获取新增前最大编号
        before_noticeid=before_result[0][0]
        # print(type(before_noticeid))
        # 登录
        con=http.client.HTTPConnection(host='localhost')
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=admin&password=admin&savelogin=true',
                    headers={"Content-Type":"application/x-www-form-urlencoded"})
        # print(con.getresponse().getheaders())
        # print(con.getresponse().getheader(name='Set-Cookie'))
        # 获取cookie
        cookies=con.getresponse().getheader(name='Set-Cookie')
        # print(cookies.split(';')[0])
        cookie1=cookies.split(';')[0]
        con.close()
        #新增公告
        con=http.client.HTTPConnection(host='localhost')
        con.request(method='POST',url='/agileone/index.php/notice/add',
                    body='headline=title&content=NNNN&scope=1&expireddate=2019-03-24',
                    headers={"Content-Type":"application/x-www-form-urlencoded","Cookie":cookie1})
        # print(con.getresponse().read().decode())
        #获取新增公告的实际编号
        actual_noticeid = con.getresponse().read().decode()
        # print(type(actual_noticeid))
        con.close()
        after_sql = "select * from notice order by noticeid desc limit 0,1"
        cur.execute(after_sql)
        after_result = cur.fetchall()
        #获取新增公告后，数据库最大的编号
        after_noticeid = after_result[0][0]
        # print(type(after_noticeid))
        # print(after_result[0][2])
        #断言新增公告，编号是否自增1
        if (int(actual_noticeid)==after_noticeid and after_noticeid == before_noticeid+1):
            print("新增公告：编号自增1，测试通过")
        else:
            print("新增公告：编号自增1，测试不通过")
        #断言新增公告，数据库内容是否正确
        if after_result[0][2]=="NNNN":
            print("新增公告：内容校验正确，测试通过")
        else:
            print("新增公告：内容校验错误，测试不通过")
        cur.close()
        db.close()

if __name__ == '__main__':
    t = Http_Test()
    # t.get_test()
    # t.post_test003()
    # t.post_test004()
    t.post_test007()
